package stopwatch;

/**
 * A StopWatch that measures elapsed time.
 * @author Natchanon Hongladaromp 5510546034
 */
public class StopWatch
{
    /** Time when stopwatch was started. */
    private long startTime;
    /** Time when stopwatch was stopped. */
    private long stopTime;
    /** Boolean isRun is true if stopwatch is running. */
    private boolean isRun;

    /** Constructor initializes a new stopwatch. */
    public StopWatch()
    {
        startTime = 0;
        stopTime = 0;
        isRun = false;
    }

    /** A start is method to start a stopwatch. */
    public void start()
    {
        if (!isRun)
        {
            startTime = System.nanoTime();
            isRun = true;
        }
    }

    /** A stop is method to stop a stopwatch. */
    public void stop()
    {
        if (isRun)
        {
            stopTime = System.nanoTime();
            isRun = false;
        }
    }

    /** 
     * A getElapsed is method to get time that passed.
     * @return time that passed after start and before stop (second).
     */
    public double getElapsed()
    {
        if (!isRun)
            return (double)(stopTime - startTime) * 1E-9;
        return (double)(System.nanoTime() - startTime) * 1E-9;
    }

    /**
     * isRunning is method for check the stopwatch.
     * @return true if running and false if not running.
     */
    public boolean isRunning()
    {
        return isRun;
    }
}
